import request from '@/utils/request'
import $ from 'jquery'
import config from '@/utils/config'
import { Message } from 'element-ui'
import store from '@/store'

export function SubmitDistrictChange (params) {
  let joinYear
  let experimentAt
  let standoutAt
  if (params.joinYear instanceof Date) {
    joinYear = params.joinYear.getFullYear()
  } else if (!isNaN(params.joinYear)) {
    joinYear = new Date(parseInt(params.joinYear)).getFullYear()
  } else {
    joinYear = 0
  }
  params.experimentAt === '' ? experimentAt = '0001-01-01T00:00:00Z' : experimentAt = params.experimentAt
  params.standoutAt === '' ? standoutAt = '0001-01-01T00:00:00Z' : standoutAt = params.standoutAt
  const data = {
    id: params.id,
    schoolNum: Number(params.schoolNum),
    isExperiment: params.isExperiment,
    joinYear: joinYear,
    contact: params.contact,
    contactPhone: params.contactPhone,
    contactEmail: params.contactEmail,
    contactAddress: params.contactAddress,
    experimentAt: experimentAt,
    standoutAt: standoutAt
  }
  return request({
    url: '/area/modify',
    method: 'post',
    data
  })
}

export function SubmitSchoolChange (params) {
  let joinAt, experimentAt, standoutAt
  params.joinAt === '' ? joinAt = '0001-01-01T00:00:00Z' : joinAt = params.joinAt
  params.experimentAt === '' ? experimentAt = '0001-01-01T00:00:00Z' : experimentAt = params.experimentAt
  params.standoutAt === '' ? standoutAt = '0001-01-01T00:00:00Z' : standoutAt = params.standoutAt
  const data = {
    id: params.id,
    name: params.name,
    isStandout: params.isStandout,
    schoolType: params.schoolType,
    headmaster: params.headmaster,
    headmasterPhone: params.headmasterPhone,
    headmasterEmail: params.headmasterEmail,
    contact: params.contact,
    contactPhone: params.contactPhone,
    contactEmail: params.contactEmail,
    joinAt: joinAt,
    studentNum: params.studentNum,
    schoolCode: params.schoolCode,
    code: params.code,
    hasApply: params.hasApply,
    homepage: params.homepage,
    officialAccounts: params.officialAccounts,
    isExperiment: params.isExperiment,
    experimentAt: experimentAt,
    standoutAt: standoutAt,
    contactAddress: params.contactAddress
  }
  return request({
    url: '/school/modify',
    method: 'post',
    data
  })
}

export function SubmitStudentChange (params) {
  let birthday
  params.birthday === '' ? birthday = '0001-01-01T00:00:00Z' : birthday = params.birthday
  const data = {
    id: params.id,
    schoolRollId: params.schoolRollId,
    name: params.name,
    sex: params.sex,
    nation: params.nation.toString(),
    birthday: birthday,
    identityId: params.identityId,
    gradeYear: params.gradeYear,
    grade: params.grade,
    class: params.class,
    studentId: params.studentId,
    phone: params.phone,
    address: params.address
  }
  return request({
    url: '/student/modify',
    method: 'post',
    data
  })
}

export function SubmitPhysicalChange (params) {
  const data = {
    id: params.id,
    gradeYear: params.gradeYear,
    grade: params.grade,
    class: params.class,
    measureDate: params.measureDate,
    isExemption: params.isExemption,
    height: params.height.toString(),
    weight: params.weight.toString(),
    vitalCapacity: params.vitalCapacity.toString(),
    fiftyMeters: params.fiftyMeters.toString(),
    sitAndReach: params.sitAndReach.toString(),
    oneMinuteRopeSkipping: params.oneMinuteRopeSkipping.toString(),
    oneMinuteSitUps: params.oneMinuteSitUps.toString(),
    shuttleRun: params.shuttleRun.toString(),
    standingLongJump: params.standingLongJump.toString(),
    chinning: params.chinning.toString(),
    oneThousandMeters: params.oneThousandMeters.toString(),
    eightHundredMeters: params.eightHundredMeters.toString(),
    bodyFat: params.bodyFat.toString(),
    grip: params.grip.toString(),
    responseCapability: params.responseCapability.toString(),
    balanceAbility: params.balanceAbility.toString(),
    verticalJump: params.verticalJump.toString()
  }
  return request({
    url: '/physical/modify',
    method: 'post',
    data
  })
}

export function SubmitLabelChange (params) {
  const data = {
    id: params.id,
    value: params.value,
    color: params.color
  }
  return request({
    url: '/label/modify',
    method: 'post',
    data
  })
}

export function SubmitPasswordChange (id, password) {
  const data = {
    id,
    password
  }
  return request({
    url: '/manager/password',
    method: 'post',
    data
  })
}

export function SubmitAuthorityChange (id, userName, name) {
  const data = {
    id,
    userName,
    name
  }
  return request({
    url: '/manager/modify',
    method: 'post',
    data
  })
}

export function SubmitDistrictAdd (params) {
  let joinYear
  let experimentAt
  let standoutAt
  if (params.joinYear instanceof Date) {
    joinYear = params.joinYear.getFullYear()
  } else if (!isNaN(params.joinYear)) {
    joinYear = new Date(parseInt(params.joinYear)).getFullYear()
  } else {
    joinYear = 0
  }
  params.experimentAt === '' ? experimentAt = '0001-01-01T00:00:00Z' : experimentAt = params.experimentAt
  params.standoutAt === '' ? standoutAt = '0001-01-01T00:00:00Z' : standoutAt = params.standoutAt
  const data = {
    provinceId: params.provinceId,
    cityId: params.cityId,
    name: params.name,
    schoolNum: Number(params.schoolNum),
    isExperiment: params.isExperiment,
    joinYear: joinYear,
    contact: params.contact,
    contactPhone: params.contactPhone,
    contactEmail: params.contactEmail,
    contactAddress: params.contactAddress,
    experimentAt: experimentAt,
    standoutAt: standoutAt
  }
  return request({
    url: '/area/add',
    method: 'post',
    data
  })
}

export function SubmitSchoolAdd (params) {
  if (params.provinceId === '') {
    params.provinceId = 0
  }
  if (params.countryId === '') {
    params.countryId = 0
  }
  if (params.experimentAreaId === '') {
    params.experimentAreaId = 0
  }
  let joinAt, experimentAt, standoutAt
  params.joinAt === '' ? joinAt = '0001-01-01T00:00:00Z' : joinAt = params.joinAt
  params.experimentAt === '' ? experimentAt = '0001-01-01T00:00:00Z' : experimentAt = params.experimentAt
  params.standoutAt === '' ? standoutAt = '0001-01-01T00:00:00Z' : standoutAt = params.standoutAt
  const data = {
    provinceId: params.provinceId,
    cityId: params.cityId,
    areaId: params.areaId,
    experimentAreaId: params.experimentAreaId,
    name: params.name,
    isStandout: params.isStandout,
    schoolType: params.schoolType,
    headmaster: params.headmaster,
    headmasterPhone: params.headmasterPhone,
    headmasterEmail: params.headmasterEmail,
    contact: params.contact,
    contactPhone: params.contactPhone,
    contactEmail: params.contactEmail,
    joinAt: joinAt,
    studentNum: params.studentNum,
    schoolCode: params.schoolCode,
    code: params.code,
    hasApply: params.hasApply,
    homepage: params.homepage,
    officialAccounts: params.officialAccounts,
    isExperiment: params.isExperiment,
    experimentAt: experimentAt,
    standoutAt: standoutAt,
    contactAddress: params.contactAddress
  }
  return request({
    url: '/school/add',
    method: 'post',
    data
  })
}

export function SubmitStudentAdd (params) {
  let birthday
  params.birthday === '' ? birthday = '0001-01-01T00:00:00Z' : birthday = params.birthday
  const data = {
    provinceId: params.provinceId,
    cityId: params.cityId,
    experimentAreaId: params.experimentAreaId,
    schoolId: params.schoolId,
    schoolRollId: params.schoolRollId,
    name: params.name,
    sex: params.sex,
    nation: params.nation.toString(),
    birthday: birthday,
    identityId: params.identityId,
    gradeYear: params.gradeYear,
    grade: params.grade,
    class: params.class.toString(),
    studentId: params.studentId,
    phone: params.phone,
    address: params.address
  }
  return request({
    url: '/student/add',
    method: 'post',
    data
  })
}

export function SubmitLabelAdd (params) {
  const data = {
    value: params.labelName,
    color: params.color
  }
  return request({
    url: '/label/add',
    method: 'post',
    data
  })
}

export function SubmitDistrictDelete (params) {
  let id = []
  params.forEach(item => {
    id.push(item.id)
  })
  const data = { id }
  return request({
    url: '/area',
    method: 'delete',
    data
  })
}

export function SubmitSchoolDelete (params) {
  let id = []
  params.forEach(item => {
    id.push(item.id)
  })
  const data = { id }
  return request({
    url: '/school',
    method: 'delete',
    data
  })
}

export function SubmitStudentDelete (params) {
  let id = []
  params.forEach(item => {
    id.push(item.id)
  })
  const data = { id }
  return request({
    url: '/student',
    method: 'delete',
    data
  })
}

export function SubmitLabelDelete (params) {
  let id = []
  params.forEach(item => {
    id.push(item.id)
  })
  const data = { id }
  return request({
    url: '/label',
    method: 'delete',
    data
  })
}

export function CheckUserName (userName, authority) {
  const data = {
    userName,
    authority
  }
  return request({
    url: '/username/validate/exist',
    method: 'post',
    data
  })
}

export function CheckHasUserName (id, type) {
  const data = {
    id,
    type
  }
  return request({
    url: '/user/has',
    method: 'post',
    data
  })
}

export function awaitCheckUserName (userName, authority, id) {
  const data = {
    id,
    userName,
    authority
  }
  let res = $.ajax({ url: config.baseUrl + '/username/validate/exist', method: 'post', contentType: 'application/json', xhrFields: { withCredentials: true }, data: JSON.stringify(data), async: false })
  handleError(res)
  return res
}

export function awaitCheckAreaName (provinceId, cityId, name) {
  const data = {
    provinceId,
    cityId,
    name
  }
  let res = $.ajax({ url: config.baseUrl + '/areaname/validate/exist', method: 'post', contentType: 'application/json', xhrFields: { withCredentials: true }, data: JSON.stringify(data), async: false })
  handleError(res)
  return res
}

export function awaitCheckSchoolCode (schoolCode, id) {
  const data = {
    id,
    schoolCode
  }
  let res = $.ajax({ url: config.baseUrl + '/schoolcode/validate/exist', method: 'post', contentType: 'application/json', xhrFields: { withCredentials: true }, data: JSON.stringify(data), async: false })
  handleError(res)
  return res
}

export function awaitCheckSchoolRollId (schoolRollId, id) {
  const data = {
    id,
    schoolRollId
  }
  let res = $.ajax({ url: config.baseUrl + '/schoolrollid/validate/exist', method: 'post', contentType: 'application/json', xhrFields: { withCredentials: true }, data: JSON.stringify(data), async: false })
  handleError(res)
  return res
}

export function awaitCheckStudentPhone (phone, id) {
  const data = {
    id,
    phone
  }
  let res = $.ajax({ url: config.baseUrl + '/studentphone/validate/exist', method: 'post', contentType: 'application/json', xhrFields: { withCredentials: true }, data: JSON.stringify(data), async: false })
  handleError(res)
  return res
}

export function awaitCheckLabel (labelName, labelColor, id) {
  const data = {
    id,
    name: labelName,
    color: labelColor
  }
  let res = $.ajax({ url: config.baseUrl + '/label/validate/exist', method: 'post', contentType: 'application/json', xhrFields: { withCredentials: true }, data: JSON.stringify(data), async: false })
  handleError(res)
  return res
}

function handleError (res) {
  if (res.status !== 200) {
    console.error('err' + res)
    Message({
      message: res,
      type: 'error',
      duration: 5 * 1000
    })
  } else {
    if (res.responseJSON.retCode === 2) {
      Message({
        message: res.responseJSON.msg,
        type: 'error',
        duration: 5 * 1000
      })
    } else if (res.responseJSON.retCode === 1) {
      store.dispatch('LogOut').then(() => {
        location.reload()
      })
    } else if (typeof res.responseJSON.retCode === 'undefined') {
      Message({
        message: '未知错误',
        type: 'error',
        duration: 5 * 1000
      })
    }
  }
}

export var nation = {
  '汉族': 1,
  '蒙古族': 2,
  '回族': 3,
  '藏族': 4,
  '维吾尔族': 5,
  '苗族': 6,
  '彝族': 7,
  '壮族': 8,
  '布依族': 9,
  '朝鲜族': 10,
  '满族': 11,
  '侗族': 12,
  '瑶族': 13,
  '白族': 14,
  '土家族': 15,
  '哈尼族': 16,
  '哈萨克族': 17,
  '傣族': 18,
  '黎族': 19,
  '傈僳族': 20,
  '佤族': 21,
  '畲族': 22,
  '高山族': 23,
  '拉祜族': 24,
  '水族': 25,
  '东乡族': 26,
  '纳西族': 27,
  '景颇族': 28,
  '柯尔克孜族': 29,
  '土族': 30,
  '达斡尔族': 31,
  '仫佬族': 32,
  '羌族': 33,
  '布朗族': 34,
  '撒拉族': 35,
  '毛难族': 36,
  '仡佬族': 37,
  '锡伯族': 38,
  '阿昌族': 39,
  '普米族': 40,
  '塔吉克族': 41,
  '怒族': 42,
  '乌孜别克族': 43,
  '俄罗斯族': 44,
  '鄂温克族': 45,
  '崩龙族': 46,
  '保安族': 47,
  '裕固族': 48,
  '京族': 49,
  '塔塔尔族': 50,
  '独龙族': 51,
  '鄂伦春族': 52,
  '赫哲族': 53,
  '门巴族': 54,
  '珞巴族': 55,
  '基诺族': 56,
  '其他': 97,
  '外国血统': 98
}

export var grade = {
  '一年级': 11,
  '二年级': 12,
  '三年级': 13,
  '四年级': 14,
  '五年级': 15,
  '六年级': 16,
  '初一': 21,
  '初二': 22,
  '初三': 23,
  '高一': 31,
  '高二': 32,
  '高三': 33
}
